//
//  RegisterRetypePasswordController.m
//  HuLaQuan
//
//  Created by hok on 1/27/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "RegisterRetypePasswordController.h"

#import "RegisterEngine.h"
#import "LoginEngine.h"
@interface RegisterRetypePasswordController ()

@property (nonatomic, strong) RegisterEngine *registerEngine;
@property (nonatomic, strong) LoginEngine *loginEngine;
@end

@implementation RegisterRetypePasswordController
- (LoginEngine *)loginEngine
{
    if(!_loginEngine) _loginEngine = [[LoginEngine alloc] init];
    return _loginEngine;
}
- (RegisterEngine *)registerEngine
{
    if (!_registerEngine) {
        _registerEngine = [[RegisterEngine alloc] init];
    }
    return _registerEngine;
}

// OverWrite
-(NSString *)finish:(NSString *)pwd{
    NSLog(@"%@",pwd);
    if([pwd isEqualToString:self.passwordAndInfos[@"password"]])
    {
        //NSLog(@"一致");
        [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
        [self.registerEngine registeUserPhone:self.passwordAndInfos[@"userName"] withUserPassword:self.passwordAndInfos[@"password"] withToken:self.passwordAndInfos[@"tokenValue"]];
    
    }else{
        [self.view makeToast:kPasswordRetypeNotEqual];
    }
    return pwd;
    
}

#pragma mark - CallBack
- (void)registerFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        /**
         *  注册后自动登录
         */
        [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
        [self.loginEngine loginWithUsername:self.passwordAndInfos[@"userName"] andPassword:self.passwordAndInfos[@"password"]];
    }else{
        
        if([messageDic[@"message"] isKindOfClass:[NSString class]] && [messageDic[@"message"] isEqualToString:@"该手机号已经注册"])
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        [self showNetWorkError:messageDic[@"message"]];
        
    }
}
- (void)loginFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"TabBar" bundle:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self presentViewController:[storyBoard instantiateViewControllerWithIdentifier:@"HomeTabBarController"] animated:NO completion:nil];
            
        });
        
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    //  });
    
}


#pragma mark - Controller Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerFinish:) name:@"RegisterFinish" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFinish:) name:@"LoginFinish" object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RegisterFinish" object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LoginFinish" object:nil];
    [super viewWillDisappear:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation
- (void)goBackAction
{
    UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"是否放弃注册新用户" andBlock:^(UIAlertAction *action ) {
        if ([action.title isEqualToString:@"好的"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        
        }
        
    }  type:@"confirmAndCancel"];
    [self presentViewController:x animated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
